package com.youhome.api.controller;

import com.youhome.api.dto.*;
import com.youhome.common.exception.CustomException;
import com.youhome.service.service.ZillowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/07/24.
 */
@RestController
@Api(tags = "分析")
@RequestMapping("/homes")
public class AnalysisController {

    @Autowired
    private ZillowService zillowService;

    @ApiOperation(value = "时间序列统计分析", notes = "时间序列统计分析", response = TimeValueItemResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "类别", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/timeseries/{type}/{zip}")
    public Object timeSeries(@PathVariable Integer type, @PathVariable String zip) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, zillowService.getTimeSeries(type, zip, 0));
    }


    @ApiOperation(value = "租房指数", notes = "租房指数", response = ZriResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "类别", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/rent-index/{type}/{zip}")
    public Object zri(@PathVariable Integer type, @PathVariable String zip) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Zip_Zri_AllHomes_Summary(type, zip));
    }

    @ApiOperation(value = "买卖指数", notes = "买卖指数", response = BuyerSellerResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/buyer-seller/{zip}")
    public Object buyerSeller(@PathVariable String zip) {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Buyer_Seller_Index_Zip(1, zip));
    }

    @ApiOperation(value = "市场健康指数", notes = "市场健康指数", response = MarketHealthResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/market-health/{zip}")
    public Object marketHealth(@PathVariable String zip) {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Market_Health_Index_Zip(4, zip));
    }

    @ApiOperation(value = "房屋价格指数", notes = "房屋价格指数", response = ZHVIResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/price-index/{zip}")
    public Object priceIndex(@PathVariable String zip) {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Zip_Zhvi_Summary_AllHomes(73, zip));
    }

    @ApiOperation(value = "租售比统计", notes = "租售比统计", response = DTOBreakEvenPRResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/sales-rent-ratio/{zip}")
    public Object salesRentalRatio(@PathVariable String zip) {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Break_Even_PR(85, zip));
    }

    @ApiOperation(value = "租金涨幅预测指数", notes = "租金涨幅预测指数", response = ZriForecastPublicResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/rent-change-forecast/{zip}")
    public Object rentChangeForecast(@PathVariable String zip) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, zillowService.get_Zri_Forecast_Public(89, zip));
    }

    @ApiOperation(value = "超大额贷款利息统计", notes = "超大额贷款利息统计", response = TimeValueItemResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/analytics/jumbo-interest-rate")
    public Object jumboInterestRate() {
        return new Result(ResultCode.SUCCESS, zillowService.get_loan_interest(97, 1));
    }

    @ApiOperation(value = "传统贷款利息统计", notes = "传统贷款利息统计", response = TimeValueItemResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/analytics/conventional-interest-rate")
    public Object conventionalInterestRate() {

        return new Result(ResultCode.SUCCESS, zillowService.get_loan_interest(98, 1));
        /*
        String type = "98";
        Integer year = 1;
        String key = "zillow/" + getType(type) + "/" + getType(type) + "_AVG.json";
        System.err.println(key);
        String json = downloadService.downloadJsonFromOSS(key);
        if (!StringUtils.isEmpty(json)) {
            List<TimeValueItemResult> timeValueItemResult = getTimeSeriesDataByYear(json, year);
            return new Result<>(ResultCode.SUCCESS, timeValueItemResult);
        }
        return new Result<>(ResultCode.SUCCESS);

        */
    }

    @ApiOperation(value = "存量房统计", notes = "存量房统计", response = DTOBreakEvenPRResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "tier", value = "等级(top、middle、bottom)", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "format", value = "格式(count、pct)", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "analytics/inventory/{tier}/{format}/{zip}")
    public Object inventory(@PathVariable String tier, @PathVariable String format, @PathVariable String zip) throws CustomException {
        return new Result(ResultCode.SUCCESS,
                zillowService.get_InventoryMeasure_Tiers_Metro_Public_By_Tier_Format(99, tier, format, zip, 10));
    }

    @ApiOperation(value = "都会负资产率统计", notes = "都会负资产率统计", response = MetroNegativeEquityRateResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/negative-equity-metro/{zip}")
    public Object negativeEquityMetro(@PathVariable String zip) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.get_Metro_NETiers(100, zip));
    }

    @ApiOperation(value = "城市负资产率统计", notes = "城市负资产率统计", response = TimeValueItemResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zip", value = "邮编", required = true, paramType = "path")
    })
    @GetMapping(value = "/analytics/negative-equity-cities/{zip}")
    public Object negativeEquityCities(@PathVariable String zip) throws CustomException {
        return new Result(ResultCode.SUCCESS, zillowService.get_NegativeEquity_Public(101, zip, 10));
    }
}
